package dao;

import com.alibaba.fastjson.JSON;
import domin.*;
import lombok.extern.slf4j.Slf4j;
import util.CommonDao;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;

@Slf4j
public class BlacklistDao {
    private static BlacklistDao blacklistDao= new BlacklistDao();
    public BlacklistDao(){}
    public static BlacklistDao getInstance(){
        return blacklistDao;
    }
    private PreparedStatement preparedStatement;
    private ResultSet resultSet;
    public Blacklist find (Connection connection, int id) throws SQLException {
        Blacklist blacklist = null;
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM blacklist WHERE id=?");
        preparedStatement.setInt(1,id);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            blacklist= new Blacklist();
            blacklist.setId(resultSet.getInt("id"));
            blacklist.setReason(resultSet.getString("reason"));
            Actor actor = ActorDao.getInstance().find(connection,resultSet.getInt("actor_id"));
            blacklist.setActor(actor);
        }
        JdbcHelper.close(resultSet,preparedStatement);
        return blacklist;
    }


    public Collection<Blacklist> findAll(Connection connection, Collection<Condition> condition, Pagination pagination) throws SQLException {

        Collection<Blacklist> blacklists = new HashSet<>();

        int totalNum = CommonDao.getInstance().count(connection,"blacklist");

        String sql_select_all = "SELECT * FROM blacklist";
        if (condition != null){
            sql_select_all += Condition.toWhereClause(JSON.toJSONString(condition));
            log.trace(sql_select_all);
        }
        if (pagination != null){

            sql_select_all += pagination.toLimitClause(totalNum);
            log.trace(sql_select_all);
        }
        this.preparedStatement = connection.prepareStatement(sql_select_all);
        this.resultSet = preparedStatement.executeQuery();
        while (this.resultSet.next()){
            Blacklist blacklist= new Blacklist();
            blacklist.setId(resultSet.getInt("id"));
            blacklist.setReason(resultSet.getString("reason"));
            Actor actor = ActorDao.getInstance().find(connection,resultSet.getInt("actor_id"));
            blacklist.setActor(actor);
            System.out.println(blacklist.toString());
            blacklists.add(blacklist);
            System.out.println(blacklist.toString());
        }
        System.out.println(blacklists.toString());

        JdbcHelper.close(this.resultSet,this.preparedStatement);

        return blacklists;
    }
    public Collection<Blacklist> getAllWithCondition(Connection connection, String s) throws SQLException{
        Collection<Blacklist> blacklists = new HashSet<Blacklist>();
        String value = "%" + s + "%";
        value=" \"" + value + "\"";
        PreparedStatement preparedStatement = connection.prepareStatement("" +
                "select * from blacklist where " +
                "reason like " + value);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            Blacklist blacklist = null;
            Integer id = resultSet.getInt("id");
            String reason = resultSet.getString("reason");
            Actor actor = ActorDao.getInstance().find(connection,resultSet.getInt("actor_id"));
            blacklist = new Blacklist(id,reason,actor);
            blacklists.add(blacklist);
        }
        JdbcHelper.close(null,preparedStatement);
        return blacklists;
    }

    public boolean add(Connection connection , Blacklist blacklist) throws SQLException {
        //添加并获得id值
        PreparedStatement preparedStatement =
                connection.prepareStatement(
                        "INSERT INTO " +
                                "blacklist (reason,actor_id)" +
                                "VALUES(?,?)"
                );
        //设置添加信息参数
        preparedStatement.setString(1,blacklist.getReason());
        preparedStatement.setInt(2,blacklist.getActor().getId());
        //执行并获得受影响行数
        int rowAffected = preparedStatement.executeUpdate();
        //关闭
        JdbcHelper.close(null,preparedStatement);
        //返回添加是否成功
        return rowAffected>0;
    }
    //删除
    public boolean delete(Connection connection,Integer id) throws SQLException {
        this.preparedStatement = connection.prepareStatement("delete from blacklist where id=" + id);
        //执行sql语句并获得受影响的行数
        int rowAffected = preparedStatement.executeUpdate();
        //关闭语句盒子、数据库连接
        JdbcHelper.close(null,preparedStatement);
        return rowAffected>0;
    }
}
